package club.yuit.desktop.ui;

import club.yuit.desktop.GlobalData;
import club.yuit.desktop.ui.brokers.BrokerListPanel;
import club.yuit.desktop.ui.topics.TopicCardListPanel;
import club.yuit.desktop.ui.topics.TopicListTabPanel;
import club.yuit.rmq.service.RMQClusterService;
import club.yuit.rmq.MQAdminFactory;
import club.yuit.rmq.config.RMQConfigure;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.remoting.exception.RemotingConnectException;
import org.apache.rocketmq.remoting.exception.RemotingSendRequestException;
import org.apache.rocketmq.remoting.exception.RemotingTimeoutException;
import org.apache.rocketmq.remoting.protocol.body.ClusterInfo;
import org.apache.rocketmq.tools.admin.MQAdminExt;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import static club.yuit.desktop.ui.UIConstants.DEFAULT_SIZE;

/**
 * @author: yuit
 * @date: 2023/11/27 17:42
 */
@Slf4j
public class MainUI extends JFrame {

    private MQAdminExt admin;
    private InitRMQAdminDialog initRMQAdminDialog;
    private RMQClusterService service;
    private JPanel mainPanel;

    private BrokerListPanel brokerListPanel;

    private String currentCluster;

    private TopicCardListPanel topicCardListPanel;

    private JTabbedPane tabPanel;


    public MainUI() throws HeadlessException {

        this.setSize(DEFAULT_SIZE);
        this.setLocationRelativeTo(null);

        ImageIcon icon = UITools.getSVGIcon("/imgs/Icon.svg",16,16);//new ImageIcon(getClass().getResource("/imgs/Icon.png"));
        this.setIconImage(icon.getImage());
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);


        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                super.windowClosed(e);
                if (admin != null) {
                    admin.shutdown();
                }
            }
        });

        mainPanel = new JPanel(){
            @Override
            public Insets getInsets() {
                return new Insets(10,10,5,10);
            }
        };

        mainPanel.setLayout(new BorderLayout());

        this.setTitle("RMQ-CLI-Desktop");

        this.add(mainPanel,BorderLayout.CENTER);
        this.setVisible(true);
        initRMQAdminDialog = new InitRMQAdminDialog(this);
        initRMQAdminDialog.showDialog();
        initTabPane();
    }





    public void initTabPane() {
        // 初始化集群列表面板
        brokerListPanel = new BrokerListPanel(service);

        tabPanel = new JTabbedPane();
        tabPanel.addTab("Brokers", brokerListPanel);
        tabPanel.addTab("Topics", new TopicListTabPanel(service));
        //brokerListPanel.updateData(this.currentCluster);
        this.mainPanel.add(tabPanel, BorderLayout.CENTER);
        this.mainPanel.updateUI();
    }








    public boolean initRMQAdmin(String version, String proxyAddress, String nameServerAddress) {
        MQAdminFactory factory = new MQAdminFactory();
        RMQConfigure c = new RMQConfigure();
        c.setNamesrvAddr(nameServerAddress);
        c.setAccessKey(System.getProperty("accessKey"));
        c.setSecretKey(System.getProperty("secretKey"));
        String errorMsg = "";
        try {
            this.admin = factory.getInstance(c);
            ClusterInfo clusterInfo = null;
            clusterInfo = admin.examineBrokerClusterInfo();
            GlobalData.MQ_DATA.put(GlobalData.MQ_CLUSTER_INF, clusterInfo);
            if(version.equals("5.X")){
                System.setProperty("endpoints", proxyAddress);
            }
            this.service = new RMQClusterService(c,admin);
            return true;
        } catch (MQClientException | InterruptedException | MQBrokerException | RemotingTimeoutException |
                 RemotingSendRequestException e) {
            errorMsg = e.getMessage();
            admin.shutdown();
        } catch (RemotingConnectException e) {
            errorMsg = "链接错误，请确认地址是否正确！";
            admin.shutdown();
        }


        return false;

        /*if (!errorMsg.isEmpty()){
            JOptionPane.showMessageDialog(this, errorMsg,"提示",JOptionPane.ERROR_MESSAGE);
        }*/
    }

    public RMQClusterService getService() {
        return service;
    }
}
